package io.github.bloquesoft.entity.clazz;

import io.github.bloquesoft.entity.definition.EntityDefinition;
import io.github.bloquesoft.entity.definition.ListPropertyDefinition;
import io.github.bloquesoft.entity.definition.PojoPropertyDefinition;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

public class ClassEntityDefinition implements EntityDefinition {

    @Getter
    private final Class<?> mappedClass;

    @Getter
    private final String id;

    @Setter
    @Getter
    private ClassPojoPropertyDefinition classPrimaryKeyProperty;

    private final List<ClassPojoPropertyDefinition> pojoPropertyList = new ArrayList<>();

    private final List<ClassListPropertyDefinition> listPropertyList = new ArrayList<>();

    public ClassEntityDefinition(String id, Class<?> mappedClass) {
        this.id = id;
        this.mappedClass = mappedClass;
    }

    public void addPojoProperty(ClassPojoPropertyDefinition property) {
        this.pojoPropertyList.add(property);
    }

    public void addListProperty(ClassListPropertyDefinition property) {
        this.listPropertyList.add(property);
    }

    public List<ClassPojoPropertyDefinition> getClassPojoPropertyList() {
        return this.pojoPropertyList;
    }

    public List<ClassListPropertyDefinition> getClassListPropertyList() {
        return this.listPropertyList;
    }


    @Override
    public PojoPropertyDefinition getPrimaryKeyProperty() {
        return this.classPrimaryKeyProperty;
    }

    @Override
    public PojoPropertyDefinition getParentPojoProperty() {
        return this.getPojoPropertyList().stream().filter(PojoPropertyDefinition::isParentId).findFirst().orElse(null);
    }

    @Override
    public List<PojoPropertyDefinition> getPojoPropertyList() {
        return new ArrayList<>(this.pojoPropertyList);
    }

    @Override
    public List<ListPropertyDefinition> getListPropertyList() {
        return new ArrayList<>(this.listPropertyList);
    }
}